﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OPMS_V1.BOL;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Net.Mail;


namespace OPMS_V1.AccessControls
{
    public partial class ForgottenPassword : System.Web.UI.Page
    {
        Register objRegBOL = new Register();
        Crypto crypt = new Crypto();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblEmailError.Visible = false;
            pnlRecordsExists.Visible = false;
            pnlEmailNotExists.Visible = false;
            lblLoginError.Visible = false;
        }

        protected void btnForgotPassword_Click(object sender, EventArgs e)
        {
            Boolean successful = false;
            string emailaddress = txtEmail.Text;
            string username=null;
            string password=null;

            if (emailaddress == "")
            {
                lblEmailError.Visible = true;
            }
            else
            {
                List<string> loginid = new List<string>();
                List<string> loginpassword = new List<string>();

                //Trying to validate the user with the Username and encrypted Password stored on to the database
                loginid = objRegBOL.ForgotUsername(emailaddress);
                loginpassword = objRegBOL.ForgotPassword(emailaddress);

                foreach(string test in loginid)
                {
                    username = test.ToString();
                }

                foreach (string test in loginpassword)
                {
                    password = test.ToString();
                }

                if (username==null && password==null)
                {
                    successful = false;
                    lblRecordNotExists.Text = "Sorry we could not find " + emailaddress + "  in our records.";
                    pnlEmailNotExists.Visible = true;
                }

                if (username != null && password != null)
                {
                    successful = true;
                    lblSentMessage.Text += " " + emailaddress;
                    pnlRecordsExists.Visible = true;
                    pnlUserDetails.Visible = false;
                }
                else
                {
                    successful = false;
                }


                if (successful == true)
                {
                    objRegBOL.SendMessage(username, password, emailaddress);
                }
            }
        }

        protected void txtEmail_TextChanged(object sender, EventArgs e)
        {
            string emailaddress = txtEmail.Text;

            if (emailaddress == "")
            {
                regexEmail.Visible = true;
                lblEmailError.Visible = true;
            }
        }

        protected void lnkForgot_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AccessControls/ForgottenPassword.aspx");
        }

        /// <summary>
        /// This function is used for Logging to the system (Authentication)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region Login Button functioning
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //Registration details parameters
            Boolean successful = false;
            string accountName = txtAccountName.Text;
            string password = crypt.Encrypt(txtPassword.Text);
            string lastLogin = System.Convert.ToString(DateTime.Now);

            //If the user enters no data in the fields, show errors
            if (accountName == "" || txtPassword.Text == "")
            {
                lblLoginError.Visible = true;
            }
            else
            {
                //Trying to validate the user with the Username and encrypted Password stored on to the database
                int response = objRegBOL.ValidateUser(accountName, password);

                if (response == 0)
                {
                    //Maitaing the session for the user logon.
                    successful = true;
                    Session["user"] = accountName;
                }
                else
                {
                    successful = false;
                    lblLoginError.Visible = true;
                }

                if (successful == true)
                {
                    //If sucessfully authenticate the system redirect to desired page
                    Response.Redirect("~//Property.aspx?Mode=buy");
                }
            }
        }
        #endregion Login Button functioning
    }
}
